const { spawn } = require('child_process');
const path = require('path');

console.log('🚀 启动多窗口测试环境...');
console.log('📝 请确保Next.js client服务器已经启动: pnpm dev');
console.log('⏳ 等待 3秒后 开始构建测试窗口...\n');

// 等待服务器启动
setTimeout(async () => {
  try {
    // Electron实例数
    const instances = 3;
    
    for (let i = 0; i < instances; i++) {
      const instanceId = i + 1;
      
      console.log(`📱 启动测试窗口 ${instanceId}...`);
      
      const electronProcess = spawn('electron', [path.join(__dirname, '../electron/main.js')], {
        env: {
          ...process.env,
          NODE_ENV: 'development',
          ELECTRON_INSTANCE_ID: instanceId.toString()
        },
        stdio: 'pipe',
        detached: false
      });
      
      electronProcess.on('error', (error) => {
        console.error(`❌ 窗口 ${instanceId} 启动失败:`, error.message);
      });
      
      electronProcess.on('exit', (code) => {
        console.log(`🔚 窗口 ${instanceId} 已关闭，code: ${code}`);
      });
      
      // 延迟启动下一个窗口
      if (i < instances - 1) {
        await new Promise(resolve => setTimeout(resolve, 2000));
      }
    }
    
    console.log('\n✅ 所有的测试窗口已经启动');
    console.log('💡 提示：您也可以使用应用内的测试工具按鈕來创建更多窗口');
  } catch (error) {
    console.error('❌ 启动失败:', error.message);
  }
}, 3000); 